package top.vains.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.vains.entity.Result;
import top.vains.entity.User;
import top.vains.service.IUserService;
import top.vains.util.JwtUtils;
import top.vains.util.ResultUtils;

/**
 * 登陆接口
 *
 * @author vains
 * @version 1.0
 * @since 2020/2/18 18:04
 **/
@RestController
@RequestMapping("/login")
@Api(tags = "登陆相关接口，包括登陆、注册和无权限")
public class LoginController {

    private final IUserService userService;

    @Autowired
    public LoginController(IUserService userService) {
        this.userService = userService;
    }

    @PostMapping("/verifyUser")
    @ApiOperation(value = "登陆接口", notes = "登陆成功之后会获取一个token字符串")
    public Result verifyUser(@RequestBody User user) {
        User login = userService.login(user.getNumber(), user.getPassword());
        if (login == null) {
            return ResultUtils.error("账号或密码错误！");
        }
        String token = JwtUtils.createJwt(0, login);
        return ResultUtils.success("登陆成功！", token);
    }

}
